#ifndef MESSAGE_H
#define MESSAGE_H

#include "types.h"
#include "string.h"
#include "null.h"

/* flags definition */
#define INT_DATA 1
#define STR_DATA 0


#define MAX_LENGTH_DATA 50

//the priority field is also the message's tag. Here are defined some defaults tags
#define SUPERVISION_MESSAGE 0

/* datastructures */

/* At the beginning, the fixed length of a message's data is 50 characters */
typedef struct{
  int8_t in_use;
  int32_t from;
  int32_t to;
  int32_t priority;
  union{
    char str_data[MAX_LENGTH_DATA];
    int int_data;
  }data;
  int8_t data_flag; // 0 if the message carries a string, 1 for an integer
}message;

/* function declarations */

/*getters*/

int32_t message_get_from(message* target);

int32_t message_get_to(message* target);

int8_t message_get_data_flag(message* target);

int32_t message_get_priority(message* target);

int8_t message_get_in_use(message* target);

/* Returns a pointer to the string stored in target if the data flag is set to string. Returns NULL otherwise. */
char* message_get_str_value(message* target);

/* Returns the integer stored in target if the data flag is set to int. Returns 0 otherwise. */
int32_t message_get_int_value(message* target);

/* setters */

void message_set_from(message* target, int32_t from);

void message_set_to(message* target, int32_t to);

void message_set_data_flag(message* target, int8_t data_flag);

void message_set_priority(message* target, int32_t priority);

void message_set_in_use(message* target, int8_t in_use);

void message_set_int_value(message* target, int32_t value);

void message_set_str_value(message* target, char* value);

/* copies the message pointed by m2 into m1 */
void messcpy(message* m1, message* m2);

#endif 
